package org.zht.dormpro.auth.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zht.dormpro.auth.bean.Tree;
import org.zht.dormpro.auth.model.SysDept;
import org.zht.dormpro.auth.service.IDeptService;
import org.zht.dormpro.pub.ResponseMap;

import java.util.List;

@Controller
@RequestMapping("/auth/dept")
public class DeptController {
    @Autowired
    private IDeptService deptService;

    @ResponseBody
    @RequestMapping("/listDeptTree")
    @RequiresPermissions("auth:dept:listDeptTree")
    public ResponseMap listDeptTree() {
        Tree<SysDept> tree = deptService.listDeptTree();

        return ResponseMap.success(tree);
    }

    @RequestMapping("treeView")
    public String treeView(){
        return "dept/deptTree";
    }

    @ResponseBody
    @RequestMapping("/listDept")
    @RequiresPermissions("auth:dept:listDept")
    public List<SysDept> listDept() {
        List<SysDept> sysDepts = deptService.listDept();
        return sysDepts;
    }

    @RequestMapping(value = "/addDept", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap addDept(SysDept dept) {
        boolean success = deptService.addDept(dept);
        if (success) {
            return ResponseMap.success("部门添加成功!");
        } else {
            return ResponseMap.error("部门添加失败!");
        }
    }


    @RequestMapping("/addDeptPage/{parentId}")
    public String addDeptPage(Model model, @PathVariable("parentId") Integer parentId) {
        model.addAttribute("parentId", parentId);
        if (parentId == 0) {
            model.addAttribute("parentName", "根目录");
        } else {
            SysDept dept = deptService.getDeptById(parentId);
            model.addAttribute("parentName", dept.getName());
        }
        return "dept/dept_add";
    }

    @RequestMapping("/listDeptPage")
    public String listDeptPage() {
        return "dept/dept_list";
    }

    @RequestMapping("/ediDeptPage/{id}")
    public String ediDeptPage(Model model, @PathVariable("id") Integer id) {
        //查询当前资源信息
        SysDept dept = deptService.getDeptById(id);
        Integer parentId = dept.getParentId();
        model.addAttribute("dept", dept);
        //查询父节点的名称
        if (parentId != 0) {
            SysDept parent = deptService.getDeptById(parentId);
            model.addAttribute("parentName", parent.getName());
        } else {
            model.addAttribute("parentName", "根目录");
        }
        return "dept/dept_edit";
    }

    @ResponseBody
    @RequestMapping(value = "/editDept", method = RequestMethod.POST)
    public ResponseMap editDept(SysDept newDept) {
        boolean success = deptService.editDept(newDept);
        if (success) {
            return ResponseMap.success("部门更新成功!");
        } else {
            return ResponseMap.error("部门更新失败!");
        }
    }

    @RequestMapping("/deleteDept")
    @ResponseBody
    public ResponseMap deleteDept(Integer deptId) {
        //删除删除部门表
        boolean success = deptService.deleteDept(deptId);
        if (success) {
            return ResponseMap.success("菜单删除成功!");
        }
        return ResponseMap.success("菜单删除失败!");
    }

}
